package com.kcwx.web.controller.monitor;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.kcwx.common.annotation.Log;
import com.kcwx.common.core.controller.BaseController;
import com.kcwx.common.core.domain.AjaxResult;
import com.kcwx.common.core.page.TableDataInfo;
import com.kcwx.common.enums.BusinessType;
import com.kcwx.common.utils.poi.ExcelUtil;
import com.kcwx.framework.web.service.SysPasswordService;

import com.kcwx.system.domain.SysLogininfor;
import com.kcwx.system.service.ISysLogininforService;

import com.kcwx.shareddomain.common.dto.DeleteDto;
import com.kcwx.shareddomain.common.dto.DelParaValid;
import com.kcwx.shareddomain.system.query.SysLogininforQuery;

/**
 * 系统访问记录
 * 
 * @author liwan
 */
@Api(tags = "系统管理-登录日志管理")
@RestController
@RequestMapping("/monitor/logininfor")
public class SysLogininforController extends BaseController
{
    @Autowired
    private ISysLogininforService logininforService;

    @Autowired
    private SysPasswordService passwordService;

    /**
     * 分页查询
     */
    @ApiOperation("获取日志分页列表")
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:list')")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody SysLogininforQuery logininfor)
    {
        startPage();
        List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
        return success(getDataTable(list));
    }
    /**
     * 批量删除登录日志
     */
    @ApiOperation("批量删除登录日志")
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "批量删除登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/batchRemove")
    public AjaxResult batchRemove(@RequestBody DeleteDto dto)
    {
        Long []ids=DelParaValid.validBatchDel(dto);
        int rowsAffect=logininforService.deleteLogininforByIds(ids);
        return rowsAffect>0?success():error("删除失败");
    }
    /**
     * 清空登录日志
     */
    @ApiOperation("清空登录日志")
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "清空登录日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public AjaxResult clean()
    {
        logininforService.cleanLogininfor();
        return success();
    }
    /**
     * 账户解锁
     */
    @ApiOperation("账户解锁")
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:unlock')")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @GetMapping("/unlock")
    public AjaxResult unlock(@RequestParam String userName)
    {
        passwordService.clearLoginRecordCache(userName);
        return success();
    }
    /**
     * 导出
     */
    @Log(title = "导出登录日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLogininforQuery logininfor)
    {
        List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
        ExcelUtil<SysLogininfor> util = new ExcelUtil<SysLogininfor>(SysLogininfor.class);
        util.exportExcel(response, list, "登录日志");
    }
}
